import { http } from "@/utils/http";

// 标签管理相关接口类型定义
export interface TagInfo {
  id: number;
  name: string;
  color: string;
  type: number;
  weight: number;
  description?: string;
  sort?: number;
  status?: number;
}

export interface ArticleInfo {
  id: number;
  title: string;
  content: string;
  image?: string;
  status: number;
  category_id?: number;
  category_name?: string;
  user_id?: number;
  author?: string;
  published_date?: string;
  audit_reason?: string;
  tags?: TagInfo[];
}

export interface CategoryInfo {
  id: number;
  name: string;
  description?: string;
}

// ====================== 标签与文章关联管理 ======================

/**
 * 获取文章的标签列表
 * @param params 查询参数
 */
export const getArticleTags = (params?: {
  article_id?: number;
  name?: string;
  category?: number;
  sort?: string;
}) => {
  return http.request<{
    code: number;
    message: string;
    data: { tags: TagInfo[] };
    total: number;
  }>("get", "/api/tag-management/tags/articles/tags", { params });
};

/**
 * 获取标签下的文章列表
 * @param params 查询参数
 */
export const getArticlesByTagId = (params: {
  targetId: number;
  pageNum?: number;
  pageSize?: number;
  title?: string;
  category?: number;
  status?: number;
  user_id?: number;
}) => {
  const { targetId, pageNum, pageSize, ...otherParams } = params;
  const queryParams = {
    tagId: targetId,
    page: pageNum,
    per_page: pageSize,
    ...otherParams
  };

  return http.request<{
    code: number;
    message: string;
    data: { articles: ArticleInfo[] };
    total: number;
  }>("get", "/api/tag-management/tags/articles", { params: queryParams });
};

/**
 * 获取标签下的商品列表
 * @param params 查询参数
 */
export const getGoodsByTagId = (params: {
  targetId: number;
  pageNum?: number;
  pageSize?: number;
  name?: string;
  category?: number;
  status?: number;
}) => {
  const { targetId, pageNum, pageSize, ...otherParams } = params;
  const queryParams = {
    tagId: targetId,
    page: pageNum,
    per_page: pageSize,
    ...otherParams
  };

  return http.request<{
    code: number;
    message: string;
    data: { products: any[] };
    total: number;
  }>("get", "/api/tag-management/tags/products", { params: queryParams });
};

/**
 * 批量更新标签状态
 * @param tagIds 标签ID列表
 * @param status 状态
 */
export const batchUpdateTagsStatus = (tagIds: number[], status: number) => {
  return http.request<{
    code: number;
    message: string;
  }>("patch", "/api/tag-management/tags/batch/status", {
    data: { tag_ids: tagIds, status }
  });
};

/**
 * 从商品角度分配标签
 * @param tagId 标签ID
 * @param productIds 要分配的商品ID数组
 */
export const assignTagsToProduct = (tagId: number | string, productIds: number[]) => {
  return http.request<{
    code: number;
    message: string;
    data: {
      tag_id: number;
      added_products?: number[];
    };
  }>("post", `/api/tag-management/tags/${tagId}/products`, {
    data: { product_ids: productIds }
  });
};

/**
 * 从商品角度移除标签
 * @param tagId 标签ID
 * @param productIds 要移除的商品ID数组
 */
export const removeTagFromProduct = (tagId: number | string, productIds: number[]) => {
  return http.request<{
    code: number;
    message: string;
    data: {
      tag_id: number;
      removed_products?: number[] | string;
    };
  }>("delete", `/api/tag-management/tags/${tagId}/products`, {
    data: { product_ids: productIds }
  });
};

/**
 * 从文章角度分配标签
 * @param tagId 标签ID
 * @param articleIds 要分配的文章ID数组
 */
export const assignTagsToArticle = (tagId: number | string, articleIds: number[]) => {
  return http.request<{
    code: number;
    message: string;
    data: {
      tag_id: number;
      added_articles?: number[];
    };
  }>("post", `/api/tag-management/tags/${tagId}/articles`, {
    data: { article_ids: articleIds }
  });
};

/**
 * 从文章角度移除标签
 * @param tagId 标签ID
 * @param articleIds 要移除的文章ID数组
 */
export const removeTagFromArticle = (tagId: number | string, articleIds: number[]) => {
  return http.request<{
    code: number;
    message: string;
    data: {
      tag_id: number;
      removed_articles?: number[] | string;
    };
  }>("delete", `/api/tag-management/tags/${tagId}/articles`, {
    data: { article_ids: articleIds }
  });
};

/**
 * 获取文章详情（包含标签信息）
 * @param articleId 文章ID
 */
export const getArticleDetail = (articleId: number) => {
  return http.request<{
    code: number;
    message: string;
    data: ArticleInfo;
  }>("get", `/api/tag-management/articles/${articleId}`);
};

// ====================== 辅助接口 ======================

/**
 * 获取可用文章列表（未关联指定标签的文章）
 * @param params 查询参数
 */
export const getAvailableArticles = (params?: {
  excludeTagId?: number;
  title?: string;
  category?: number;
  status?: number;
  page?: number;
  per_page?: number;
}) => {
  return http.request<{
    code: number;
    message: string;
    data: { articles: ArticleInfo[] };
    total: number;
  }>("get", "/api/tag-management/articles", { params });
};

/**
 * 获取文章分类列表
 */
export const getArticleCategories = () => {
  return http.request<{
    code: number;
    message: string;
    data: { categories: CategoryInfo[] };
  }>("get", "/api/tag-management/article-categories");
};

/**
 * 获取标签列表
 * @param params 查询参数
 * include_relations?: 是否包含关联关系具体信息
 */
export const getTags = (params?: {
  name?: string;
  type?: number;
  status?: number | string;
  page?: number;
  per_page?: number;
  include_relations?: boolean;
}) => {
  return http.request<{
    code: number;
    message: string;
    data: { list?: TagInfo[] };
    total: number;
  }>("get", "/api/tag-management/tags", { params });
};

/**
 * 创建标签
 * @param data 标签数据
 */
export const createTag = (data: Partial<TagInfo>) => {
  return http.request<{
    code: number;
    message: string;
    data: TagInfo;
  }>("post", "/api/tag-management/tags", { data });
};

/**
 * 更新标签
 * @param tagId 标签ID
 * @param data 标签数据
 */
export const updateTag = (tagId: number, data: Partial<TagInfo>) => {
  return http.request<{
    code: number;
    message: string;
    data: TagInfo;
  }>("put", `/api/tag-management/tags/${tagId}`, { data });
};

/**
 * 删除标签、批量删除标签
 * @param tagIds 标签ID数组列表
 */
export const batchDeleteTags = (tagIds: number[]) => {
  return http.request<{
    code: number;
    message: string;
  }>("delete", "/api/tag-management/tags", {
    data: { tag_ids: tagIds }
  });
};

/**
 * 更新标签状态
 * @param tagId 标签ID
 * @param status 状态
 */
export const updateTagStatus = (tagId: number, status: number) => {
  return http.request<{
    code: number;
    message: string;
  }>("put", `/api/tag-management/tags/${tagId}/status`, {
    data: { status }
  });
};

/**
 * 获取标签统计信息
 * @param tagId 标签ID
 */
export const getTagStats = (tagId: number) => {
  return http.request<{
    code: number;
    message: string;
    data: {
      article_count: number;
      product_count: number;
      total_associations: number;
    };
  }>("get", `/api/tag-management/tags/${tagId}/stats`);
};

// ====================== 批量操作 ======================

/**
 * 批量为文章分配标签
 * @param articleIds 文章ID列表
 * @param tagIds 标签ID列表
 */
export const batchAssignTagsToArticles = (articleIds: number[], tagIds: number[]) => {
  return http.request<{
    code: number;
    message: string;
  }>("post", "/api/tag-management/tags/batch/assign", {
    data: { article_ids: articleIds, tag_ids: tagIds }
  });
};

/**
 * 批量从文章中移除标签
 * @param articleIds 文章ID列表
 * @param tagIds 标签ID列表
 */
export const batchRemoveTagsFromArticles = (articleIds: number[], tagIds: number[]) => {
  return http.request<{
    code: number;
    message: string;
  }>("delete", "/api/tag-management/tags/batch/remove", {
    data: { article_ids: articleIds, tag_ids: tagIds }
  });
};

/**
 * 批量更新标签权重
 * @param updates 更新数据
 */
export const batchUpdateTagWeights = (updates: Array<{ id: number; weight: number }>) => {
  return http.request<{
    code: number;
    message: string;
  }>("patch", "/api/tag-management/tags/batch/weights", {
    data: { updates }
  });
};

// ====================== 导入导出 ======================

/**
 * 导出标签数据
 * @param params 导出参数
 */
export const exportTags = (params?: { type?: number; status?: number; format?: "excel" | "csv" }) => {
  return http.request<Blob>("get", "/api/tag-management/tags/export", {
    params,
    responseType: "blob"
  });
};

/**
 * 导入标签数据
 * @param file 文件
 */
export const importTags = (file: File) => {
  const formData = new FormData();
  formData.append("file", file);

  return http.request<{
    code: number;
    message: string;
    data: {
      success_count: number;
      error_count: number;
      errors?: string[];
    };
  }>("post", "/api/tag-management/tags/import", {
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};
